package org.common4j2ee.stock.web.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.common4j2ee.stock.entity.user.Authority;
import org.common4j2ee.stock.entity.user.Role;
import org.common4j2ee.stock.dao.utils.HibernateUtils;
import org.common4j2ee.stock.dao.utils.Page;
import org.common4j2ee.stock.dao.utils.PropertyFilter;
import org.common4j2ee.stock.service.user.AuthorityManager;
import org.common4j2ee.stock.service.user.RoleManager;
import org.common4j2ee.stock.service.utils.ServiceException;
import org.common4j2ee.stock.web.utils.CrudActionSupport;
import org.common4j2ee.stock.web.utils.PropertyFilterWeb;
import org.common4j2ee.stock.web.utils.Struts2Utils;
import org.springframework.beans.factory.annotation.Autowired;


@Namespace("/user")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "role!list.action", type = "redirect") })
public class RoleAction extends CrudActionSupport<Role> {

	private Role role; 
	
	private Long id;
	
	private List<Long> checkedAuthIds;
	
	private String authrityTree;
	
	private Page<Role> page = new Page<Role>(10);//每页5条记录

	
	@Autowired
	RoleManager roleManager;
	@Autowired
	AuthorityManager authManager;

	private static final long serialVersionUID = 633445360169800367L;
	
	
	//-- CRUD Action 函数 --//
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilterWeb.buildFromHttpRequest(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		page = roleManager.searchRole(page, filters);
		return SUCCESS;
	}
	

	@Override
	public Role getModel() {
		return role;
	}

	@Override
	public String input() throws Exception {
		checkedAuthIds = role.getAuthorityIdList();
		authrityTree=authManager.getAuthrityTree(checkedAuthIds);
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		HibernateUtils.mergeByCheckedIds(role.getAuthorityList(), checkedAuthIds, Authority.class);

		roleManager.saveRole(role);
		addActionMessage("保存角色成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		try {
			roleManager.deleteRole(id);
			addActionMessage("删除角色成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage("删除角色失败");
		}
		return RELOAD;
	}
	
	//-- 其他Action函数 --//
	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkRoleName() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newLoginName = request.getParameter("name");
		String oldLoginName = request.getParameter("oldRoleName");

		if (roleManager.isRoleNameUnique(newLoginName, oldLoginName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		//因为直接输出内容而不经过jsp,因此返回null.
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id==null){
			role=new Role();
		}else{
			role=roleManager.getRole(id);
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	
	
//	public List<Authority> getAllAuthList() {
//		return authManager.getAllAuthList();
//	}


	public Role getRole() {
		return role;
	}


	public void setRole(Role role) {
		this.role = role;
	}


	public List<Long> getCheckedAuthIds() {
		return checkedAuthIds;
	}


	public void setCheckedAuthIds(List<Long> checkedAuthIds) {
		this.checkedAuthIds = checkedAuthIds;
	}


	public Page<Role> getPage() {
		return page;
	}


	public void setPage(Page<Role> page) {
		this.page = page;
	}


	public String getAuthrityTree() {
		return authrityTree;
	}


	public void setAuthrityTree(String authrityTree) {
		this.authrityTree = authrityTree;
	}
	

}
